//获取应用实例
import {request} from "../../request/index.js";
const app = getApp()
var util = require('../../utils/util.js');
Page({
  data: {
    marks:[
      {
      latitude:28.689650,
      longitude:116.031010,
      title:"当前位置",
      iconpath: "static\icon\mark.png",
      width:35,
      height:45,
    }
  ],
  time:[],
  swiperList:[],
  indexList:[
      {
        id:0,
        url:"/pages/book/book",
        name:"预约",
        path:"http://119.45.228.239:8080/images/pre.png",
        bind:"tapTobook",
    },
      {
        id:1,
        url:"/pages/seek/seek",
        name:"找车",
        path:"http://119.45.228.239:8080/images/seek.png",
        bind:"tapToseek",
      },
      {
        id:2,
        url:"/pages/scan/scan",
        name:"扫码",
        path:"http://119.45.228.239:8080/images/scan.png",
        bind:"scanCode",
    },
      {
        id:3,
        url:"/pages/more/more",
        name:"更多服务",
        path:"http://119.45.228.239:8080/images/more.png",
        bind:"tapTomore",
    },
  ],
    newsList:[],
  parkmostList:[],
  },
  //事件处理函数
  tapTolocation: function() {
    wx.switchTab({
      url: '/pages/location/location',
    })
  },
  tapToseek: function(){
    wx.navigateTo({
      url: '/pages/seek/seek',
    })
  },
  tapToscan: function(){
    wx.navigateTo({
      url: '/pages/scan/scan',
    })
  },
  tapTomore: function(){
    wx.navigateTo({
      url: '/pages/moreservice/moreservice',
    })
  },
  tapTonews: function(){
    wx.navigateTo({
      url: '/pages/news/news',
    })
  },
  tapTobook: function(){
    wx.navigateTo({
      url: '/pages/book/book',
    })
  },
  scanCode: function () {
    var that = this;
    wx.scanCode({ 
      success: (res) => { 
        console.log(res);
        let parkid = res.result;
        // let i = url.lastIndexOf("/");
        // let parkInfoId = url.substring(i+1, url.length);      
        //跳转到相应页面
        wx.navigateTo({
          url: '/pages/book/book_detail/book_detail?parkid=' + parkid
        })     
      },
      fail: (res) =>{
        wx.showToast({
          title: '扫描失败',
          icon:'none',
          duration: 2000
        })
      },
      complete: (res) => { }
    })
 },
 
  onLoad: function () {
    wx.request({
      url: 'http://localhost:8080/ipark/parkcount/count',
      data:{userid:'1'},
      header: { 
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    },
       method: 'POST',
      success: (result) => {
         this.setData({
          parkmostList: result.data.data.parkcount
         })
      },
    });
    this.getSwiperList();
   this.getNewsList();
    // var that = this;
    // wx.request({
    //   url: 'url',
    //   method:"GET",
    //   header:{

    //   },
    //   data:{

    //   },
    //   success:(res)=>{
    //     console.log(res);
    //     that.setData({
    //       swiper: res.data
    //     })
    //   }
    // })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getSwiperList(){
    request({ url: "http://localhost:8080/ipark/index/swiper"})
            .then(result => {
              this.setData({
                swiperList: result.data.data.swiperList             
            })
    })
  },
  getNewsList(){
    request({ url: "http://localhost:8080/ipark/news/indexnews"})
            .then(result => {
              var date = new Date(date);
              let dateList = result.data.data.indexNewsList.map(v=>v.createdate);
              for(let c in dateList){
              let  date = util.formatTime(dateList[c])
              dateList[c] = date
           }
              this.setData({
                time:[...this.data.time,...dateList],
                newsList: result.data.data.indexNewsList             
            })
    })
  },
  broadCast(e){
    //console.log(e.currentTarget.dataset.newsid);
    var newsid=e.currentTarget.dataset.newsid;
    request({
      url:"http://localhost:8080/ipark/voice/broadcast",
      data:{newsid}
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  // cardSwiper
  cardSwiper(e) {
    this.setData({
      cardCur: e.detail.current
    })
  },
})
